//
//  Colors.swift
//  BasicTools
//
//  Created by Ting Han on 2022/3/12.
//

import Foundation
import UIKit

extension UIColor {
    convenience init(hex3: UInt16, alpha: CGFloat = 1) {
        let divisor: CGFloat = 15
        let r = CGFloat(hex3 & 0xF00 >> 8) / divisor
        let g = CGFloat(hex3 & 0x0F0 >> 4) / divisor
        let b = CGFloat(hex3 & 0x00F ) / divisor
        
        if #available(iOS 10.0, *) {
            self.init(displayP3Red: r, green: g, blue: b, alpha: alpha)
        } else {
            self.init(red: r, green: g, blue: b, alpha: alpha)
        }
    }
    
    convenience init(hex4: UInt16) {
        let divisor: CGFloat = 15
        let r = CGFloat(hex4 & 0xF000 >> 12) / divisor
        let g = CGFloat(hex4 & 0x0F00 >> 8) / divisor
        let b = CGFloat(hex4 & 0x00F0 >> 4) / divisor
        let alpha = CGFloat(hex4 & 0x000F) / divisor
        
        if #available(iOS 10.0, *) {
            self.init(displayP3Red: r, green: g, blue: b, alpha: alpha)
        } else {
            self.init(red: r, green: g, blue: b, alpha: alpha)
        }
    }
    
    convenience init(hex6: UInt32, alpha: CGFloat = 1) {
        let divisor = CGFloat(255)
        let red     = CGFloat((hex6 & 0xFF0000) >> 16) / divisor
        let green   = CGFloat((hex6 & 0x00FF00) >>  8) / divisor
        let blue    = CGFloat( hex6 & 0x0000FF       ) / divisor
        
        if #available(iOS 10.0, *) {
            self.init(displayP3Red: red, green: green, blue: blue, alpha: alpha)
        } else {
            self.init(red: red, green: green, blue: blue, alpha: alpha)
        }
    }
    
    convenience init(hex8: UInt32) {
        let divisor = CGFloat(255)
        let red     = CGFloat((hex8 & 0xFF000000) >> 24) / divisor
        let green   = CGFloat((hex8 & 0x00FF0000) >> 16) / divisor
        let blue    = CGFloat((hex8 & 0x0000FF00) >>  8) / divisor
        let alpha   = CGFloat( hex8 & 0x000000FF       ) / divisor
        
        if #available(iOS 10.0, *) {
            self.init(displayP3Red: red, green: green, blue: blue, alpha: alpha)
        } else {
            self.init(red: red, green: green, blue: blue, alpha: alpha)
        }
    }
}

enum UIColorInputError : Error {
    case missingHashMarkAsPrefix
    case unableToScanHexValue
    case mismatchedHexStringLength
}

extension UIColor {
    
}


extension UIColor {
    func hexString(_ includeAlpha: Bool) -> String {
        var r: CGFloat = 0
        var g: CGFloat = 0
        var b: CGFloat = 0
        var a: CGFloat = 0
        self.getRed(&r, green: &g, blue: &b, alpha: &a)
        
        if (includeAlpha) {
            return String(format: "#%02X%02X%02X%02X", Int(r * 255), Int(g * 255), Int(b * 255), Int(a * 255))
        } else {
            return String(format: "#%02X%02X%02X", Int(r * 255), Int(g * 255), Int(b * 255))
        }
    }
    
    open override var description: String {
        return self.hexString(true)
    }
    
    open override var debugDescription: String {
        return self.hexString(true)
    }
}
